/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package privatewhiteboard.client.whiteboard;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bui Thi Mai
 */
public class ListItem extends javax.swing.JPanel {
    private MyFile file;
    
    /**
     * Creates new form ListItem
     */
    public ListItem(MyFile file) {
        initComponents();
        
        this.file = file;
        jlItemTitle.setText(file.Title);
        jbtnOpenFile.addActionListener(new OpenFileListener());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jlItemTitle = new javax.swing.JLabel();
        jbtnOpenFile = new javax.swing.JButton();

        jlItemTitle.setText("Title");

        jbtnOpenFile.setText("Open");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jlItemTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(331, Short.MAX_VALUE)
                .addComponent(jbtnOpenFile)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jlItemTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnOpenFile))
        );
    }// </editor-fold>                        

    private class OpenFileListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(!new File(file.FilePath).exists()){
                 int i = 1;
            }
            else{
                try {
                    Desktop.getDesktop().open(new File(file.FilePath));
                } catch (IOException ex) {
                    Logger.getLogger(ListItem.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jbtnOpenFile;
    public javax.swing.JLabel jlItemTitle;
    // End of variables declaration                   
}

